#!/usr/bin/env python
# coding:utf-8
# 用于查询全服玩家的角色名信息

import pymysql
import sqlite3
import sys
reload(sys)
sys.setdefaultencoding("utf8")

# sid,uid,cid
# 这个列表中的每个值，由sid、uid与cid的组合
# 次序分别 sid，uid，cid
cid_list = [
101000091072011619004811,
101000031071982213010850,
101000051070207211007110,
101000051070366715008198,
101000051070417315008648,
101000051071924815010989,
101000081071811718007397,
101000091071723419003340,
101000011069155911007783,
101000011069283211008581,
101000011071807511013374,
101000031069937011013895,
101000031070762213009671,
101000051070289411007599,
101000051070362615008163,
101000061069211416007127,
101000061072069916007096,
101000061072096316007140,
101000081071811718007397,
101000081071927118007887,
101000081072062118008457,
101000091071402419000552,
101000091071411819000640,
101000091071464919001142,
101000091072011619004811,
101000011068978111008432,
101000011068989111013488,
101000011069155911007783,
101000011069283211008581,
101000011069302811008645,
101000011069571611009293,
101000011070073111010825,
101000011070136811011093,
101000011070184911011406,
101000011070690411013470,
101000011071807511013374,
101000011071820511013381,
101000011072021611013471,
101000011072035211013483,
101000011072049711013490,
101000021070984612001323,
101000021071986712002420,
101000031068555313010750,
101000031068835311007326,
101000031068837111009894,
101000031068880911009175,
101000031068989111012025,
101000031068996511007321,
101000031068997711008986,
101000031069406911009317,
101000031069486211010258,
101000031069937011013895,
101000031070054411014523,
101000031070061311014559,
101000031070410613009170,
101000031070545713009240,
101000031070715313009604,
101000031070717113009608,
101000031070720713009616,
101000031070721113009617,
101000031070723913009623,
101000031070762213009671,
101000031071512713010701,
101000031071982213010850,
101000031071986713010852,
101000041071289414007550,
101000051068784711007756,
101000051070207211007110,
101000051070289411007599,
101000051070362615008163,
101000051070417315008648,
101000051070423415008700,
101000051070592915010165,
101000051071924815010989,
101000061069211416007127,
101000061072008016007101,
101000061072069916007096,
101000061072096316007140,
101000061072097216007146,
101000071070690417007647,
101000081068978018007766,
101000081068982318008568,
101000081070596518008356,
101000081071411818007128,
101000081071461918007208,
101000081071692018007127,
101000081071811718007397,
101000081071871818007626,
101000081071927118007887,
101000081071937218007935,
101000081071954318008140,
101000081071986718008270,
101000081071986918008089,
101000081072024818008293,
101000081072024918008294,
101000081072038718008334,
101000081072045918008375,
101000081072050718008397,
101000081072057618008432,
101000081072061018008454,
101000081072062118008457,
101000081072096218008570,
101000091068889019001087,
101000091071188519000600,
101000091071326219000114,
101000091071402419000552,
101000091071411819000640,
101000091071417919000702,
101000091071420819000736,
101000091071437819000888,
101000091071444519000954,
101000091071461919001111,
101000091071464919001142,
101000091071494019001393,
101000091071669719002908,
101000091071723419003340,
101000091071771619003734,
101000091071778319003959,
101000091071785419003812,
101000091071792019003836,
101000091071826319004001,
101000091071865019004142,
101000091071900319004327,
101000091071963619004569,
101000091071973219004623,
101000091071986719004779,
101000091072008019004796,
101000091072011619004811
]

cid_list = set(cid_list)

def execMysqlCommand(host,port,user,passwd,dbname,query):
    '''
    查询数据库，返回结果为嵌套元组
    '''
    conn = pymysql.connect(host=host, port=port, user=user, passwd=passwd, db=dbname, charset="utf8")
    cur = conn.cursor()
    cur.execute(query)
    result = cur.fetchall()
    cur.close()
    conn.close()
    return result

# 拉取所有游戏服数据库信息
query = "SELECT DISTINCT sdbip,sdbport,sdbname,real_sid,real_sname FROM t_gameserver_list;"
server_list = execMysqlCommand('10.66.143.17', 3306, 'root', '123456', 'Login', query)

for sdbip,sdbport,sdbname,real_sid,real_sname in server_list:
    query = "SELECT DISTINCT c_cid,c_uid,c_charname from t_char_basic;"
    every_gs = execMysqlCommand(sdbip, sdbport, 'root', '123456', sdbname, query)
    for c_cid,c_uid,c_charname in every_gs:
        key = "{0}{1}{2}".format(real_sid, c_uid, c_cid)
        if int(key) in cid_list:
            print "'{0}\t{1}\t{2}".format(key, c_charname, real_sname)
